﻿package Interface.BattlesClasses
{	
	import Interface.SelectableLabel;
	import Fonts.FranclinGothicDemi;
	import GameUtils.BattleRequestData;
	import GameUtils.TrackData;
	
	import flash.display.Sprite;
	import flash.text.TextField;	
	import flash.text.TextFormat;		
	import flash.events.Event;
	
	public class BattleLabel extends SelectableLabel
	{		
		private var FCover:Sprite;
		private var FBackground:Sprite;
		private var FData:BattleRequestData;
		private var FIsBot:Boolean;
		private var FRating:int;
		private var FTextMovingForward:Boolean;
		
		public function BattleLabel(AData:BattleRequestData):void
		{
			FData = AData;
			FRating = FData.Rating;
			var vFont:FranclinGothicDemi = new FranclinGothicDemi();
			var vFormat:TextFormat = new TextFormat(vFont.fontName);
			vFormat.size = 14;			
			vFormat.align = 'center';
			
			FBackground = new Sprite();			
			addChild(FBackground);
			
			FText = new TextField();						
			FText.embedFonts = true;
			FText.defaultTextFormat = vFormat;			
			FText.selectable = false;			
			FText.text = FData.UserName + ' - ' + FData.Track.Name;
			FText.height = 26;
			FText.width = Math.max(433, FText.textWidth);
			if(FText.width > 433)
				FText.width += 32;
			FText.textColor = 0xFFFFFF;
			addChild(FText);
			
			FCover = new Sprite();
			FCover.graphics.beginFill(0x000000, 0);
			FCover.graphics.drawRect(0, 0, width, height);
			FCover.graphics.endFill();
			FCover.buttonMode = true;
			FCover.useHandCursor = true;
			addChild(FCover);
			
			FBackground.graphics.beginFill(0x000000, 0.5);
			FBackground.graphics.drawRect(0, 0, width, height);
			FBackground.graphics.endFill();
			
			FIsBot = false;
			FTextMovingForward = false;
			
			if(FText.textWidth > 433)
				addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			
			super();
		}	
		
		private function OnEnterFrame(e:Event):void
		{
			if(FTextMovingForward)
			{
				if(FText.x < 0)
					FText.x++;
				else
					FTextMovingForward = false;
			}
			else
			{
				if(FText.x > -(FText.width - 433))
					FText.x--;
				else
					FTextMovingForward = true;
			}
		}
		
		public function get Track():TrackData
		{
			return FData.Track;
		}
		
		public function get Data():BattleRequestData
		{
			return FData;
		}
		
		public function get UID():String
		{
			return FData.UID;
		}
		
		public function set UID(AUID:String):void
		{
			FData.UID = AUID;
		}
		
		public function get IsBot():Boolean
		{
			return FIsBot;
		}
		
		public function get Rating():int
		{
			return FRating;
		}
		
		public function get Name():String
		{
			if(FData)
				return FData.UserName;
			else
				return '';
		}
		
		public function set IsBot(AIsBot:Boolean):void
		{
			FIsBot = AIsBot;
		}
	}	
}
